package com.wesmart.mvvm.share

import android.app.ActivityOptions
import android.app.SharedElementCallback
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.skyjing.rvdadapter.CommonAdapter
import com.skyjing.askit.LogUtils
import com.skyjing.astools.R
import com.skyjing.astools.databinding.ActivitySharaElementBinding
import com.skyjing.astools.databinding.ShareItemListBinding
import com.skyjing.astools.share.ShareBean
import kotlinx.android.synthetic.main.activity_shara_element.rv

class ShareActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var binding: ActivitySharaElementBinding
    private var bundle: Bundle? = null
    private val list = ArrayList<ShareBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
//        getWindow().setExitTransition( Slide(Gravity.LEFT));
        super.onCreate(savedInstanceState)
        binding = ActivitySharaElementBinding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)
//        binding.btn1.setOnClickListener(this)
//        binding.btn2.setOnClickListener(this)

        if (Build.VERSION.SDK_INT >= 22) {
            setExitSharedElementCallback(object : SharedElementCallback() {
                override fun onMapSharedElements(
                    names: MutableList<String>?,
                    sharedElements: MutableMap<String, View>?
                ) {
                    super.onMapSharedElements(names, sharedElements)
                    LogUtils.e("共享元素", "ShareActivity item 11111111111111111")
                    if (bundle != null) {
                        val i: Int = bundle!!.getInt("index", 0)
                        val cliclk: Int = bundle!!.getInt("click", 0)
                        val type: Int = bundle!!.getInt("type", 0)
                        sharedElements?.clear()
                        names?.clear()
//                        val itemView: View = lm.findViewByPosition(i)
//                        val imageView: ImageView = itemView.findViewById(R.id.imageView)
                        //注意这里第二个参数，如果防止是的条目的item则动画不自然。放置对应的imageView则完美
//                        LogUtils.e("共享元素", "ShareActivity item$i")
//                        if (i == 2) {
////                            val btn = appCompatButton5
//                            sharedElements?.put(("item$i"), btn2)
//
//                        } else {
//                            sharedElements?.put(("item$i"), btn1)
//                            LogUtils.e("共享元素", "ShareActivity item$i")
//                        }
//                        bundle = null

                        if (rv.layoutManager?.findViewByPosition(i) != null){
                            val itemView: View = rv.layoutManager?.findViewByPosition(i)!!

                            when(type){
                                1->{
                                    when(cliclk){
                                        1->{
                                            val imageView: ImageView = itemView.findViewById(R.id.tvItem1)
                                            sharedElements?.put(("item$i"), imageView)
                                        }
                                        2->{
                                            val imageView: ImageView = itemView.findViewById(R.id.tvItem2)
                                            sharedElements?.put(("item$i"), imageView)
                                        }
                                        3->{
                                            val imageView: ImageView = itemView.findViewById(R.id.tvItem3)
                                            sharedElements?.put(("item$i"), imageView)
                                        }
                                    }
                                }
                                2->{
                                    val imageView: ImageView = itemView.findViewById(R.id.tvItem1)
                                    sharedElements?.put(("item$i"), imageView)
                                }
                            }

                        }
                        bundle = null
                    }
                }
            })
        }

        initList()
        rv.apply {
            layoutManager = LinearLayoutManager(this@ShareActivity)
            adapter = initAdapter()
        }
    }

    private fun initAdapter(): CommonAdapter<ShareBean, ShareItemListBinding> {
        return object :
            CommonAdapter<ShareBean, ShareItemListBinding>(this, R.layout.share_item_list, list) {
            override fun convert(v: ShareItemListBinding, t: ShareBean, position: Int) {
                v?.tv?.text = t.name

                when(t.type){
                    1->{

                    }
                    2->{
                        v.tvItem2.visibility = View.GONE
                        v.tvItem3.visibility = View.GONE
                    }
                }
                v.tvItem1.setOnClickListener {
                    click(v.tvItem1 , position , 1 , t.type)
                }
                v.tvItem2.setOnClickListener {
                    click(v.tvItem2 , position , 2, t.type)
                }
                v.tvItem3.setOnClickListener {
                    click(v.tvItem3 , position , 3, t.type)
                }
            }
        }
    }

    private fun click(imageView: ImageView , position: Int, cliclk: Int , type:Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            var intent = Intent(this, ShareTwoActivity::class.java)
            intent.putExtra("index", position)
            intent.putExtra("click" , cliclk)
            intent.putExtra("type" , type)
//            intent.putStringArrayListExtra("list", listTemp)
            startActivity(
                intent,
                ActivityOptions.makeSceneTransitionAnimation(
                    this,
                    imageView, "item$position"
                ).toBundle()
            )
        } else {
            startActivity(Intent(this, ShareTwoActivity::class.java))
        }
    }

    private fun initList() {
        for (i in 0..100) {
            val bean = ShareBean()
            bean.name = "$i"
            if (i % 5 == 0){
                bean.type = 2
            }else{
                bean.type = 1
            }
            list.add(bean)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        LogUtils.e("共享元素", "onActivityResult")
    }

    override fun onActivityReenter(resultCode: Int, data: Intent?) {
        super.onActivityReenter(resultCode, data)
        bundle = Bundle(data!!.extras)
        LogUtils.e("共享元素", "onActivityReenter")

//        try {
//            linearLayoutManager.scrollToPosition(position)
//            //这里需要延迟等待新的itemView被绘制才能执行上面设置的setExitSharedElementCallback转场动画
//            activity?.supportPostponeEnterTransition()
//            mActivity.window.decorView.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
//                override fun onPreDraw(): Boolean {
//                    mActivity.window.decorView.viewTreeObserver.removeOnPreDrawListener(this)
//                    activity?.supportStartPostponedEnterTransition()
//                    return false
//                }
//            })
//        }catch (e : java.lang.Exception){
//
//        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn1 -> {
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                    var intent = Intent(this, ShareTwoActivity::class.java)
//                    intent.putExtra("index", 1)
//                    startActivity(
//                        intent,
//                        ActivityOptions.makeSceneTransitionAnimation(
//                            this,
//                            btn1, "item1"
//                        ).toBundle()
//                    )
////                    startActivity(intent,
////                            ActivityOptions.makeSceneTransitionAnimation(this@ShareActivity,
////                                    btn1, "wang").toBundle())
//                } else {
//                    startActivity(Intent(this, ShareTwoActivity::class.java))
//                }
            }
            R.id.btn2 -> {
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                    var intent = Intent(this, ShareTwoActivity::class.java)
//                    intent.putExtra("index", 2)
//                    startActivity(
//                        intent,
//                        ActivityOptions.makeSceneTransitionAnimation(
//                            this,
//                            btn2, "item2"
//                        ).toBundle()
//                    )
////                    startActivity(intent,
////                            ActivityOptions.makeSceneTransitionAnimation(this@ShareActivity,
////                                    appCompatButton3, "wang").toBundle())
//                } else {
//                    startActivity(Intent(this, ShareTwoActivity::class.java))
//                }
            }
        }
    }
}